$(function() {
	$("label").click(function() {
		$("label").removeClass("on");
		$(this).addClass("on");
	});
});
window.onload = function() {
	var use = document.getElementById("use");
	var core=document.getElementById("core");
	
	use.onmousemove=function(){
		core.style.display="block";
	}
	core.onmousemove=function(){
		core.style.display="block";
	}
	use.onmouseout=function(){
		core.style.display="none";
	}
	core.onmouseout=function(){
		core.style.display="none";
	}
	
	var box = document.getElementsByClassName("box")[0];

	function createDIV(num) {
		for(var i = 0; i < num; i++) {
			var pawDiv = document.createElement("div");
			pawDiv.className = "pawDiv";
			box.appendChild(pawDiv);
			var paw = document.createElement("input");
			paw.type = "password";
			paw.className = "paw";
			paw.maxLength = "1";
			paw.readOnly = "readonly";
			pawDiv.appendChild(paw);
		}
	}
	createDIV(6);

	var pawDiv = document.getElementsByClassName("pawDiv");
	var paw = document.getElementsByClassName("paw");
	var pawDivCount = pawDiv.length;
	/*设置第一个输入框默认选中*/
	paw[0].readOnly = false;
	paw[0].focus();

	var errorPoint = document.getElementsByClassName("errorPoint")[0];
	/*绑定pawDiv点击事件*/

	function func() {
		for(var i = 0; i < pawDivCount; i++) {
			pawDiv[i].addEventListener("click", function() {
				pawDivClick(this);
			});
			paw[i].onkeyup = function(event) {
				if((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) {
					/*输入0-9*/
					changeDiv();

				} else if(event.keyCode == "8") {
					/*退格回删事件*/
					firstDiv();

				} else if(event.keyCode == "13") {
					/*回车事件*/
					getPassword();

				} else {
					/*输入非0-9*/
					errorPoint.style.display = "block";
					this.value = "";
				}

			};
		}

	}
	func();

	/*定义pawDiv点击事件*/
	var pawDivClick = function(e) {
		for(var i = 0; i < pawDivCount; i++) {
			pawDiv[i].setAttribute("style", "border-bottom: none;");
		}
		e.setAttribute("style", "border-bottom: 1px solid #ccc;");
	};

	/*定义自动选中下一个输入框事件*/
	var changeDiv = function() {
		for(var i = 0; i < pawDivCount; i++) {
			if(paw[i].value.length == "1") {
				/*处理当前输入框*/
				paw[i].blur();

				/*处理上一个输入框*/
				paw[i + 1].focus();
				paw[i + 1].readOnly = false;
				pawDivClick(pawDiv[i + 1]);
			}
		}
	};

	/*回删时选中上一个输入框事件*/
	var firstDiv = function() {
		for(var i = 0; i < pawDivCount; i++) {
			console.log(i);
			if(paw[i].value.length == "0") {
				/*处理当前输入框*/
				console.log(i);
				paw[i].blur();

				/*处理上一个输入框*/
				paw[i - 1].focus();
				paw[i - 1].readOnly = false;
				paw[i - 1].value = "";
				pawDivClick(pawDiv[i - 1]);
				break;
			}
		}
	};
	var ok = document.getElementById("ok");
	ok.onclick = function() {
		var conf = confirm('是否确认付款？');
		if(conf) {
			ok.href = "pay_success.html";
		} else {
			ok.href = "pay_failure.html";
		}
	}
}